class MyArrayBlockingQueue{
    private String[] array=null;
    public MyArrayBlockingQueue(int size){
        array=new String[size];
    }
    private Object locker=new Object();
    private int head;
    private int tail;
    private int size;
    public void put(String elem) throws InterruptedException {
        synchronized (locker){
            if(size>=array.length){
               locker.wait();
            }
            array[tail]=elem;
            tail++;
            if(tail>=array.length){
                tail=0;
            }
            size++;
            locker.notify();
        }
    }
    public String take() throws InterruptedException {
        String elem=null;
        synchronized (locker){
           if(size<=0){
                locker.wait();
            }
            elem=array[head];
            head++;
            if(head>=array.length){
                head=0;
            }
            size--;
            locker.notify();
        }
        return elem;
    }
}
public class Test13 {
    public static void main(String[] args) throws InterruptedException {
        MyArrayBlockingQueue queue=new MyArrayBlockingQueue(100);
        Thread t1=new Thread(()->{
            int n=1;
            while(true){
                try {
                    queue.put(n+"");
                    System.out.println("入队列"+n);
                    n++;

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2=new Thread(()->{
            while(true){
                try {
                    String elem=queue.take();
                    System.out.println("出队列"+elem);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
